﻿using System;

using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;

using Ah.Testudin.Engine.Events;
using Ah.Testudin.Engine.Events.Components;
using Ah.Testudin.Engine.Graphics.Scenes.SceneNodes;
using Ah.Testudin.Engine.Graphics.Scenes;

namespace Ah.Testudin.Engine.Actors.Components.Render
{
    public class ModelRenderComponent : IRenderComponent
    {
        #region Constructors

        public ModelRenderComponent(
            Actor owner,
            Model model,
            RenderPass renderPass,
            IEventManager eventManager)
        {
            if (owner == null)
            {
                throw new ArgumentNullException("owner");
            }

            if (model == null)
            {
                throw new ArgumentNullException("model");
            }

            if (eventManager == null)
            {
                throw new ArgumentNullException("eventManager");
            }

            this.Owner = owner;

            TransformComponent transformComponent =
                (TransformComponent)this.Owner.GetComponent<TransformComponent>();

            if (transformComponent == null)
            {
                throw new Exception("ModelRenderComponent can only be used with actor's that have transform components");
            }

            eventManager.TriggerEvent(
                new RenderComponentCreatedEvent(
                    this.Owner.Id,
                    new ModelSceneNode(
                        this.Owner.Id,
                        renderPass,
                        transformComponent.Transform,
                        model,
                        eventManager)));
        }

        #endregion

        #region IRenderComponent

        public Actor Owner { get; private set; }

        public void Update(GameTime gameTime)
        {
        }

        public void Dispose()
        {
        }

        #endregion
    }
}
